package com.example.memento.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 词典词条实体
 * 核心业务领域对象，包含单词的完整信息
 */
@Table("word_entries")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class WordEntry {
    
    @Id(keyType = KeyType.Auto)
    private Long id;
    
    @NotBlank(message = "单词不能为空")
    @Size(max = 100, message = "单词长度不能超过100个字符")
    private String word;
    
    @Size(max = 200, message = "发音长度不能超过200个字符")
    private String pronunciation;
    
    @Size(max = 500, message = "翻译长度不能超过500个字符")
    private String translation;
    
    @Size(max = 1000, message = "定义长度不能超过1000个字符")
    private String definition;
    
    @Size(max = 2000, message = "例句长度不能超过2000个字符")
    private String example;
    
    @Size(max = 100, message = "词性长度不能超过100个字符")
    private String partOfSpeech;
    
    @NotBlank(message = "词典源不能为空")
    @Size(max = 50, message = "词典源长度不能超过50个字符")
    private String source;
    
    @Size(max = 50, message = "语言长度不能超过50个字符")
    private String language;
    
    @Size(max = 50, message = "目标语言长度不能超过50个字符")
    private String targetLanguage;
    
    private List<String> tags;
    
    private Integer frequency;
    
    private Double confidence;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    /**
     * 计算相关性分数
     */
    public Double calculateRelevanceScore(String query) {
        if (query == null || query.trim().isEmpty()) {
            return 0.0;
        }
        
        String lowerQuery = query.toLowerCase();
        String lowerWord = word.toLowerCase();
        
        // 精确匹配得分最高
        if (lowerWord.equals(lowerQuery)) {
            return 1.0;
        }
        
        // 包含匹配
        if (lowerWord.contains(lowerQuery)) {
            return 0.8;
        }
        
        // 翻译匹配
        if (translation != null && translation.toLowerCase().contains(lowerQuery)) {
            return 0.6;
        }
        
        // 定义匹配
        if (definition != null && definition.toLowerCase().contains(lowerQuery)) {
            return 0.4;
        }
        
        return 0.0;
    }
    
    /**
     * 检查是否匹配查询
     */
    public boolean matches(String query) {
        if (query == null || query.trim().isEmpty()) {
            return false;
        }
        
        String lowerQuery = query.toLowerCase();
        return word.toLowerCase().contains(lowerQuery) ||
               (translation != null && translation.toLowerCase().contains(lowerQuery)) ||
               (definition != null && definition.toLowerCase().contains(lowerQuery));
    }
    
    /**
     * 设置创建时间戳
     * 在MyBatis-Flex中，我们需要手动调用此方法
     */
    public void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }
    
    /**
     * 设置更新时间戳
     * 在MyBatis-Flex中，我们需要手动调用此方法
     */
    public void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
}
